# -*- coding: utf-8 -*-
"""

author:zjsxxzh
"""

import socket
import threading

inString = ''
outString = ''
nick = ''


def DealOut(s):
    global nick, outString
    while True:
        outString = input()
        outString = nick + ': ' + outString
        s.send(outString.encode('utf-8'))


def DealIn(s):
    global inString
    while True:
        try:
            inString = s.recv(1024)
            if not inString:
                break
            if outString != inString:
                print(inString.decode('utf-8'))
        except:
            break


nick = input("input your nickname: ")
ip = input("input the server's ip adrress: ")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 8478))
sock.send(nick.encode('utf-8'))

thin = threading.Thread(target=DealIn, args=(sock,))
thin.start()
thout = threading.Thread(target=DealOut, args=(sock,))
thout.start()

# sock.close()